/*
 * @Author: szx
 * @Date: 2021-11-13 11:42:05
 * @LastEditTime: 2021-11-13 12:28:50
 * @Description:
 * @FilePath: \leetcode\200-299\200\200.js
 */


const a = [1, 0, -1, 0];
const b = [0, 1, 0, -1];
/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function (grid) {
    let cnt = 0;
    for (let i = 0; i < grid.length; i++) {
        for (let j = 0; j < grid[0].length; j++) {
            if (grid[i][j] == '1') {
                cnt++;
                dfs(grid, i, j);
            }
        }
    }
    return cnt;
};
function dfs(grid, i, j) {
    grid[i][j] = '0';
    for (let m = 0; m < 4; m++) {
        const mx = i + a[m],
            my = j + b[m];
        while (mx >= 0 && mx < grid.length && my >= 0 && my < grid[0].length && grid[mx][my] == '1') {
            dfs(grid, mx, my);
        }
    }
}
